package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.controller.param.BookingParam;
import com.bestcem.xm.om.dao.UserBookingDao;
import com.bestcem.xm.om.dao.mapper.UserBookingMapper;
import com.bestcem.xm.om.entity.UserBooking;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述信息
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2022/2/22 14:54
 */
@Slf4j
@Repository
public class UserBookingDaoImpl implements UserBookingDao {
    @Resource
    private UserBookingMapper userBookingMapper;

    @Override
    public UserBooking getById(String id) {
        if (!ObjectId.isValid(id)) {
            return null;
        }
        return userBookingMapper.findById(id);
    }

    @Override
    public List<UserBooking> listAll() {
        return userBookingMapper.findAll();
    }

    @Override
    public Long rejectBooking(String id, String reason) {
        return userBookingMapper.rejectBooking(id, reason, LocalDateTime.now());
    }

    @Override
    public Long countByCondition(BookingParam bookingParam, List<String> industryIds) {
        // 非超管，页面有行业筛选条件时,如果筛选条件不在用户权限内，直接返回
        if (StringUtils.isNotBlank(bookingParam.getIndustryId()) && Objects.nonNull(industryIds)
                && !industryIds.contains(bookingParam.getIndustryId())) {
            // 不在权限内
            return 0L;
        }
        return userBookingMapper.countByCondition(bookingParam, industryIds);
    }

    @Override
    public List<UserBooking> pageUserBooking(BookingParam bookingParam, List<String> industryIds) {
        // 非超管，页面有行业筛选条件时，如果筛选条件不在用户权限内，直接返回
        if (StringUtils.isNotBlank(bookingParam.getIndustryId())
                && Objects.nonNull(industryIds) && !industryIds.contains(bookingParam.getIndustryId())) {
            return Collections.emptyList();
        }

        Integer page = bookingParam.getPage();
        Integer size = bookingParam.getSize();
        if (ObjectUtils.anyNotNull(page, size)) {
            bookingParam.setOffset((page - 1) * size);
        } else {
            bookingParam.setOffset(0);
            bookingParam.setSize(10000);
        }

        return userBookingMapper.findByCondition(bookingParam, industryIds);
    }

    @Override
    public List<UserBooking> pageExportUserBooking(BookingParam bookingParam, List<String> industryIds) {
        // 非超管，页面有行业筛选条件时，如果筛选条件不在用户权限内，直接返回
        if (StringUtils.isNotBlank(bookingParam.getIndustryId())
                && Objects.nonNull(industryIds) && !industryIds.contains(bookingParam.getIndustryId())) {
            return Collections.emptyList();
        }

        Integer page = bookingParam.getPage();
        Integer size = bookingParam.getSize();
        if (ObjectUtils.anyNotNull(page, size)) {
            bookingParam.setOffset((page - 1) * size);
        } else {
            bookingParam.setOffset(0);
            bookingParam.setSize(15000);
        }

        return userBookingMapper.findByCondition(bookingParam, industryIds);
    }

    @Override
    public boolean existsByContactAndIndustryId(String contact, String id) {
        return userBookingMapper.findByContactAndIndustryId(id, contact) > 0;
    }

    @Override
    public UserBooking insert(UserBooking userBooking) {
        userBooking.setId(ObjectId.next());
        userBookingMapper.insert(userBooking);
        return userBooking;
    }

    @Override
    public boolean updateByIdAndStatus(String id, List<Integer> oldStatuses, UserBooking userBooking) {
        userBooking.setUpdateTime(LocalDateTime.now());
        return userBookingMapper.updateByIdAndStatus(id, oldStatuses, userBooking);
    }

    @Override
    public Integer updateByCustomerId(UserBooking userBooking) {
        return userBookingMapper.updateByCustomerId(userBooking);
    }

    @Override
    public Integer updateByDistributions(String key, String mail) {
        return userBookingMapper.updateByDistributions(key, mail);
    }

    @Override
    public List<String> listDistribution() {
        return userBookingMapper.listDistribution();
    }

    @Override
    public void updateByOrgId(UserBooking userBooking) {
        userBooking.setUpdateTime(LocalDateTime.now());
        userBookingMapper.updateByOrgId(userBooking);
    }

    @Override
    public List<UserBooking> listByPushStatus() {
        return userBookingMapper.listByPushStatus();
    }

    @Override
    public List<UserBooking> listByOrgIds(List<String> orgIdList) {
        if (CollectionUtils.isEmpty(orgIdList)) {
            return Collections.emptyList();
        }
        return userBookingMapper.listByOrgIds(orgIdList);
    }

    @Override
    public void updatePushStatusByIds(List<String> bookIds) {
        if (!CollectionUtils.isEmpty(bookIds)) {
            userBookingMapper.updatePushStatusByIds(bookIds);
        }
    }
}

